﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RedHouse.Data.Service;
using RedHouse.Data.Entities;
using RedHouse.Data.Common;

namespace RedHouse.Data.Base
{
    public class UserAccountService : IUserAccountService
    {

        private NewRedHouseEntitiesDataContext _dataContext;
        private static IUserAccountService _instance;
        public static IUserAccountService Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new UserAccountService();
                }
                return _instance;
            }
        }

        public UserAccountService()
        {
            _dataContext = new NewRedHouseEntitiesDataContext();
        }

        #region IUserAccountService Members

        public Common.OperationResult SaveUserAccount(Entities.RedHouseUserAccount account)
        {
            OperationResult result = new OperationResult()
            {
                Message = string.Empty,
                 Result = true,
                 ReturnObject = account
            };
            try
            {
                if (GetAccountByUserId(account.UserId) != null)
                {
                    result.Message = "Account Id already existed";
                    result.Result = false;
                }
                else
                {
                    UserAccount savedAccount = new UserAccount()
                    {
                        UserID = account.UserId,
                        Password= account.Password,
                        DisplayName = account.DisplayName,
                        CreatedBy = account.CreatedBy,
                        CreatedDate = account.CreatedDate
                    };
                    _dataContext.UserAccounts.InsertOnSubmit(savedAccount);
                    _dataContext.SubmitChanges();
                }
            }
            catch (Exception ex)
            {
                result.Message = ex.Message;
                result.Result = false;
            }
            return result;
        }

        public Entities.RedHouseUserAccount GetAccountByUserId(string userId)
        {
            UserAccount accountMaster = _dataContext.UserAccounts.Where(u => u.UserID.ToLower().Equals(userId.ToLower().Trim())).FirstOrDefault();
            if (accountMaster != null)
            {
                RedHouseUserAccount account = new RedHouseUserAccount()
                {
                    UserId = accountMaster.UserID,
                    Password = accountMaster.Password,
                    DisplayName = accountMaster.DisplayName,
                    CreatedDate = accountMaster.CreatedDate.HasValue ? accountMaster.CreatedDate.Value : DateTime.Now,
                    CreatedBy = accountMaster.CreatedBy,
                    UserDetail = null
                };
                return account;
            }
            return null;
        }

        public Common.OperationResult Login(Entities.RedHouseUserAccount account)
        {
            OperationResult result = new OperationResult()
            {
                Result = false
            };

            RedHouseUserAccount loginAccount = GetAccountByUserId(account.UserId);
            if (loginAccount == null)
            {
                return result;
            }

            if (loginAccount.Password.Equals(account.Password))
            {
                result.Result = true;
                result.ReturnObject= loginAccount;
            }
            return result;
        }

        #endregion
    }
}
